package com.design.pattern.behavior.chainofresponnsibility.service;

import org.apache.commons.lang3.StringUtils;


public class Test {


    public static class ValidateHandler extends Handler {

        @Override
        public void doHandler(Member member) {
            if(StringUtils.isEmpty(member.getLoginName())||
                    StringUtils.isEmpty(member.getPassword())) {
                System.out.println("用户名或密码不能为空。");
                return;
            }
            System.out.println("用户名和密码校验成功，可以往下执行。");
            next.doHandler(member);
        }
    }
    public static class LoginHandler extends Handler {
        
        @Override
        public void doHandler(Member member) {
            System.out.println("登录成功");
            member.setRoleName("管理员");
            next.doHandler(member);
        }
    }
    
    public static class Builder {
        private Handler head;
        private Handler tail;
        
        public Builder addHandler(Handler handler) {
            if(this.head == null) {
                this.head = this.tail = handler;
                return this;
            }
            this.tail.next = handler;
            this.tail = handler;
            return this;
        }
        public Handler build(){
            return head;
        }
    }

    public static void main(String[] args) {
        Builder builder = new Builder();
        builder.addHandler(new ValidateHandler())
                .addHandler(new LoginHandler());

        builder.build().doHandler(new Member());
    }
}
